#include <QtGui>
#include <QDebug>
#include "highlighter.h"

Highlighter::Highlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
{
    HighlightingRule rule;

    keywordFormat.setForeground(QColor(255,255,255));
    operatorFormat.setForeground(QColor(220,150,170));
    braceFormat.setForeground(QColor(255,255,255));
    defclassFormat.setForeground(QColor(255,0,255));
    stringFormat.setForeground(QColor(210,240,210));
    stringFormat.setFontWeight(QFont::Bold);
    commentFormat.setForeground(QColor(100,255,100));
    numbersFormat.setForeground(QColor(130,190,255));
    alegriaFormat.setForeground(QColor(250,200,200));
    //numbersFormat.setFontWeight(QFont::Bold);

    QStringList alegriaWords;
    alegriaWords << "owner" << "Alegria" << "GetComponent";

    foreach (const QString &pattern, alegriaWords) {
        rule.pattern = QRegExp(QString("\\b")+pattern+"\\b");
        rule.format = alegriaFormat;
        highlightingRules.append(rule);
    }

    QStringList keywordPatterns;
    keywordPatterns << "and" << "assert" << "break" << "class" <<
                       "continue" << "def" << "del" << "elif" <<
                       "else" << "except" << "exec" << "finally" <<
                       "for" << "from" << "global" << "if" << "import" <<
                       "in" << "is" << "lambda" << "not" << "or" << "pass" <<
                       "print" << "raise" << "return" << "try" <<
                       "while" << "yield" << "None" << "True" << "False";

    foreach (const QString &pattern, keywordPatterns) {
        rule.pattern = QRegExp(QString("\\b")+pattern+"\\b");
        rule.format = keywordFormat;
        highlightingRules.append(rule);
    }




    QStringList operators;
    operators << "=" << "==" << "!=" << "<" <<
                 "<=" << ">" << ">=" << "\\+" <<
                 "-" << "\\*" << "/" << "\\%" <<
                 "\\*\\*" << "//" << "\\+=" << "-=" << "\\*=" <<
                 "/=" << "\\%=" << "\\^" << "\\|" << "\\&" << "\\~" <<
                 "<<" << ">>";

    foreach (const QString &pattern, operators) {
        rule.pattern = QRegExp(pattern);
        rule.format = operatorFormat;
        highlightingRules.append(rule);
    }




    QStringList braces;
    braces << "\\{" << "\\}" << "\\(" << "\\)" <<
              "\\[" << "\\]";

    foreach (const QString &pattern, braces) {
        rule.pattern = QRegExp(pattern);
        rule.format = braceFormat;
        highlightingRules.append(rule);
    }
    /*

 rule.pattern = QRegExp("\\bdef\\b\\s*(\\w+)");
 rule.format = defclassFormat;
 highlightingRules.append(rule);

 rule.pattern = QRegExp("\\bclass\\b\\s*(\\w+)");
 rule.format = defclassFormat;
 highlightingRules.append(rule);*/

    rule.pattern = QRegExp("\\bself\\b");
    rule.format = selfFormat;
    highlightingRules.append(rule);

    rule.pattern = QRegExp("\".*\"");
    rule.format = stringFormat;
    highlightingRules.append(rule);

    rule.pattern = QRegExp("'.*'");
    rule.format = stringFormat;
    highlightingRules.append(rule);

    rule.pattern = QRegExp("#[^\n]*");
    rule.format = commentFormat;
    highlightingRules.append(rule);

    rule.pattern = QRegExp("\\b[+-]?[0-9]+[lL]?\\b");
    rule.format = numbersFormat;
    highlightingRules.append(rule);

    rule.pattern = QRegExp("\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b");
    rule.format = numbersFormat;
    highlightingRules.append(rule);

    rule.pattern = QRegExp("\\b[+-]?[0-9]+(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b");
    rule.format = numbersFormat;
    highlightingRules.append(rule);


}

void Highlighter::highlightBlock(const QString &text)
{
    foreach (const HighlightingRule &rule, highlightingRules) {
        QRegExp expression(rule.pattern);
        int index = expression.indexIn(text);
        while (index >= 0) {
            int length = expression.matchedLength();
            setFormat(index, length, rule.format);
            index = expression.indexIn(text, index + length);
        }
    }

}
